package com.ruoyi.business.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.common.Result;
import com.ruoyi.business.entity.ChatContent;
import com.ruoyi.business.entity.HomestayInfo;
import com.ruoyi.business.entity.User;
import com.ruoyi.business.entity.dto.Friends;
import com.ruoyi.business.entity.dto.FriendsDTO;
import com.ruoyi.business.entity.dto.SellerFriends;
import com.ruoyi.business.entity.dto.SellerFriendsDTO;
import com.ruoyi.business.entity.redis.UserObj;
import com.ruoyi.business.service.FrontChatContentService;
import com.ruoyi.business.service.FrontHomestayInfoService;
import com.ruoyi.business.service.FrontUserService;
import com.ruoyi.business.utils.UserObjHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ao
 * @since 2024-01-17
 */
@RestController
@RequestMapping("/business/user/chatContent")
@Api(value = "聊天信息相关接口")
public class FrontChatContentController {

    @Autowired
    private FrontChatContentService chatContentService;

    @Autowired
    private FrontHomestayInfoService homestayInfoService;

    @Autowired
    private FrontUserService userService;

    //获取我的联系表信息
    @GetMapping("/mySellers")
    public Result<List<FriendsDTO>> getMySeller(@Param("userid") String userid){
//        String userid = UserObjHolder.getUserObj().getUserid();
        if(userid==null){
            return Result.error("参数获取失败!");
        }
        List<Friends> friends = chatContentService.getFriends(userid);
        List<FriendsDTO> list = new ArrayList<>();

        for(Friends friend:friends){
            FriendsDTO friendsDTO = BeanUtil.copyProperties(friend, FriendsDTO.class);
            HomestayInfo home = homestayInfoService.getById(friend.getHomestayId());
            if(home!=null){
                friendsDTO.setHomestayname(home.getHomestayName());
                friendsDTO.setPhoto(home.getPictures());
                list.add(friendsDTO);
            }

        }

        //处理为读内容条数
        for(FriendsDTO friend:list){
            //查询未读条数
            LambdaQueryWrapper<ChatContent> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ChatContent::getReceiverUserid,userid)
                    .eq(ChatContent::getSenderUserid,friend.getFriendId())
                    .eq(ChatContent::getHomestayId,friend.getHomestayId());
            List<ChatContent> list1 = chatContentService.list(queryWrapper);
            friend.setUnreadCount(list1.size());
        }
        return Result.success(list);


    }

    //获取聊天记录
    @ApiOperation(value = "获取聊天记录")
    @GetMapping("/chatRecords")
    public Result<List<ChatContent>> getContent(
            @Param("myuserid") String myuserid,
            @Param("friendid") String friendid,
            @Param("homestayId") Long homestayId
            ){
        if(myuserid==null || friendid==null || homestayId==null){
            return Result.error("参数获取失败");
        }
        List<ChatContent> content = chatContentService.getContent(myuserid, friendid, homestayId);
        
        //修改状态
        Integer integer = chatContentService.updateReadStatus(myuserid, friendid, homestayId);



        return Result.success(content);


    }

    //获取商人的好友列表
    @ApiOperation(value = "商人的好友列表")
    @GetMapping("/getMyFriend")
    public Result<List<SellerFriendsDTO>> getMyFriend(@Param("myuserid") String myuserid){
        List<SellerFriends> sellerFriends = chatContentService.getSellerFriends(myuserid, 1);
        List<SellerFriendsDTO> list = new ArrayList<>();

        //补全其他信息
        for (SellerFriends friends:sellerFriends){
              //获取用户信息
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getUserid,friends.getFriendAccount());
            User one = userService.getOne(queryWrapper);
            if(one!=null){
                SellerFriendsDTO friendsDTO = BeanUtil.copyProperties(friends, SellerFriendsDTO.class);
                friendsDTO.setUsername(one.getUsername());
                friendsDTO.setPhoto(one.getPhoto());
                list.add(friendsDTO);

            }

        }

        //处理未读条数
        //处理为读内容条数
        for(SellerFriendsDTO friend:list){
            //查询未读条数
            LambdaQueryWrapper<ChatContent> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ChatContent::getReceiverUserid,myuserid)
                    .eq(ChatContent::getSenderUserid,friend.getFriendAccount())
                    .eq(ChatContent::getReceiverType,1);

            List<ChatContent> list1 = chatContentService.list(queryWrapper);
            friend.setUnreadMessagesCount(list1.size());
        }
        return Result.success(list);


    }

}

